﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using IndustrialSmokeCalc.SmokeCalcWCF;
using Telerik.Windows.Controls;

namespace IndustrialSmokeCalc
{
    public partial class ChooseChimney : PhoneApplicationPage
    {
        private ObservableCollection<Chimney> ListChimney;
        private Chimney SelectedChimney;

        public ChooseChimney()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if(listChimney.Items.Count == 0) busyIndicator.IsRunning = true;
            GetCompanyList();
        }

        private void GetCompanyList()
        {
            SmokeCalcClient client = new SmokeCalcClient();
            client.GetChimneyListCompleted += GetChimneyListCompleted;
            //client.GetChimneyListAsync(DataSingleton.Instance.CompanyId);
        }

        private void GetChimneyListCompleted(object sender, GetChimneyListCompletedEventArgs e)
        {
            busyIndicator.IsRunning = false;
            Thread.Sleep(500);
            ListChimney = e.Result;
            if (ListChimney.Count == 0)
            {
                txtEmpty.Visibility = Visibility.Visible;
            }
            else listChimney.ItemsSource = ListChimney;
        }

        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            Search();
        }

        private void Search()
        {
            var listSearch = new ObservableCollection<Chimney>();
            if (!txtSearch.Text.IsEmpty())
            {
                txtEmpty.Visibility = Visibility.Collapsed;
                foreach (var chimney in ListChimney)
                {
                    if (chimney.ChimneyName.ToLower().Contains(txtSearch.Text.ToLower())
                        || chimney.ChimneyDescription.ToLower().Contains(txtSearch.Text.ToLower()))
                    {
                        listSearch.Add(chimney);
                    }
                }
                listChimney.ItemsSource = listSearch;
                if (listSearch.Count == 0)
                {
                    txtEmpty.Visibility = Visibility.Visible;
                }
            }
            else
            {
                txtEmpty.Visibility = Visibility.Collapsed;
                listChimney.ItemsSource = ListChimney;
            }
        }

        private void btnChooseChimney_Click(object sender, RoutedEventArgs e)
        {
            if (listChimney.SelectedItem != null)
            {
                SelectedChimney = listChimney.SelectedItem as Chimney;
                DataSingleton.Instance.CompanyId = SelectedChimney.CompanyId;
                NavigationService.Navigate(new Uri("/Form.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("Pilih cerobong terlebih dahulu!");
            }
        }

        private void btnAddChimney_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddChimney.xaml", UriKind.Relative));
        }

        private void TxtSearch_OnActionButtonTap(object sender, EventArgs e)
        {
            Search();
        }
    }
}